package soundwave.com.demo.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import soundwave.com.demo.R;
import soundwave.com.demo.entry.LoginSingle;

/**
 * Created by xu on 2017/4/3.
 */

public class Login extends Activity {
    private TextView back;
    private TextView register;
    private EditText phone;
    private EditText word;
    private Button login;
    private TextView wangjimima;
    private String username, password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        initView();


    }

    private void initView() {
        back = (TextView) findViewById(R.id.back);
        register = (TextView) findViewById(R.id.register);
        phone = (EditText) findViewById(R.id.phone);
        word = (EditText) findViewById(R.id.word);
        login = (Button) findViewById(R.id.login);
        wangjimima = (TextView) findViewById(R.id.wangjimima);

    }


    @Override
    protected void onResume() {
        super.onResume();
        final LoginSingle loginsingle = LoginSingle.getInstance();
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                username = phone.getText().toString();
                password = word.getText().toString();
                if (username.equals("")) {
                    Toast.makeText(Login.this, "手机号码不能为空", Toast.LENGTH_SHORT).show();
                } else {
                    if (password.length() < 6) {
                        Toast.makeText(Login.this, "密码不能少于6位", Toast.LENGTH_SHORT).show();
                    } else {
                        SharedPreferences share = getSharedPreferences("login", MODE_APPEND);
                        SharedPreferences.Editor editor = share.edit();
                        editor.putBoolean("isLogin", true);
                        editor.putString("isName", "已登录");
                        editor.putString("Phone", username);
                        editor.commit();
                        loginsingle.setLogin(true);
                        loginsingle.setName("已登录");
                        loginsingle.setPhone(username);
                        Toast.makeText(Login.this, "登录成功", Toast.LENGTH_SHORT).show();
                        finish();
                    }
                }
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivityForResult(new Intent(Login.this, Register.class), 0);
            }
        });
        wangjimima.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(Login.this, Retrieve_password.class));
            }
        });

        phone.setText(loginsingle.getPhone());

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case 1:
                finish();
                break;
        }

    }
}
